/************************************************************
   Copyright 2008 Abhinav Pobbati

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*************************************************************/

#include "HkPrerequisites.h"
#include "Havok4Xna/Common/Base/HkArray.h"

namespace Havok4Xna {
	generic <class T> where T:INativeReference
	HkArray<T>::HkArray() {
		_list = gcnew List<T>();
		_array = new hkArray<void*>();
	}
	generic <class T> where T:INativeReference
	HkArray<T>::HkArray(int size) {
		_list = gcnew List<T>(size);
		for(int i = 0; i < size; i++)
			_list->Add(T());
		_array = new hkArray<void*>(size);
	}
	generic <class T> where T:INativeReference
	HkArray<T>::!HkArray() {
		if(_array != nullptr && hkBaseSystem::isInitialized()) {
			delete _array;
			_array = nullptr;
		}
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::Clear() {
		_list->Clear();
		_array->clear();
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::OptimizeCapacity(int numFreeElemsLeft) {
		_array->optimizeCapacity(numFreeElemsLeft);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::OptimizeCapacity(int numFreeElemsLeft, bool exact) {
		_array->optimizeCapacity(numFreeElemsLeft, exact);
		_list->TrimExcess();
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::RemoveAt(int index) {
		_list->RemoveAt(index);
		_array->removeAt(index);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::RemoveAtAndCopy(int index) {
		_array->removeAtAndCopy(index);
	}
	generic <class T> where T:INativeReference
	int HkArray<T>::IndexOf(T element) {
		return _array->indexOf(element->GetNativePointer());
	}
	generic <class T> where T:INativeReference
	int HkArray<T>::LastIndexOf(T element) {
		return _array->lastIndexOf(element->GetNativePointer());
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::PopBack() {
		PopBack(1);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::PopBack(int numElementsToRemove) {
		_array->popBack(numElementsToRemove);
		while(numElementsToRemove > 0) {
			_list->RemoveAt(_list->Count - 1);
			numElementsToRemove--;
		}
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::PushBack(T element) {
		_array->pushBack(element->GetNativePointer());
		_list->Add(element);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::PushBackUnchecked(T element) {
		_array->pushBackUnchecked(element->GetNativePointer());
		_list->Add(element);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::Reserve(int n) {
		_array->reserve(n);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::ReserveExactly(int n) {
		_array->reserveExactly(n);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::SetSizeUnchecked(int size) {
		_array->setSizeUnchecked(size);
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::InsertAt(int index, T element) {
		_array->insertAt(index, element->GetNativePointer());
		_list->Insert(index, element);
	}
	generic <class T> where T:INativeReference
	int HkArray<T>::Size::get() {
		return _array->getSize();
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::Size::set(int size) {
		_array->setSize(size);
	}
	generic <class T> where T:INativeReference
	int HkArray<T>::Capacity::get() {
		return _array->getCapacity();
	}
	generic <class T> where T:INativeReference
	bool HkArray<T>::IsEmpty::get() {
		return _array->isEmpty();
	}
	generic <class T> where T:INativeReference
	bool HkArray<T>::Locked::get() {
		return _array->isLocked();
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::Locked::set(bool lock) {
		_array->setLocked(lock);
	}
	generic <class T> where T:INativeReference
	T HkArray<T>::default::get(int index) {
		return _list[index];
	}
	generic <class T> where T:INativeReference
	void HkArray<T>::default::set(int index, T element) {
		(*_array)[index] = element->GetNativePointer();
		_list[index] = element;
	}
}
